package fr.vahren.dotastrat.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import org.json.simple.JSONObject;

import fr.vahren.dotastrat.Main;

public class D2SClientNetworkManager {

	private static D2SClientNetworkManager instance;

	private static final int SERVER_PORT = 19025;

	public static D2SClientNetworkManager getInstance() {
		if (instance == null) {
			instance = new D2SClientNetworkManager();
		}
		return instance;
	}

	@SuppressWarnings("unchecked")
	public void sendMove(String gameId, String playerId, String heroId,
			String destId, boolean finalAction) {
		JSONObject msg = new JSONObject();
		msg.put("gameId", gameId);
		msg.put("playerId", playerId);
		msg.put("action", "act");
		JSONObject action = new JSONObject();
		action.put("hero", heroId);
		action.put("actionType", "move");
		action.put("destination", destId);
		action.put("final", finalAction);
		msg.put("chosenAction", action);

		send(msg.toJSONString());
	}

	@SuppressWarnings("unchecked")
	public void sendAttack(String gameId, String playerId, String heroId,
			String targetId, boolean finalAction) {
		JSONObject msg = new JSONObject();
		msg.put("gameId", gameId);
		msg.put("playerId", playerId);
		msg.put("action", "act");
		JSONObject action = new JSONObject();
		action.put("hero", heroId);
		action.put("actionType", "attack");
		action.put("target", targetId);
		action.put("final", finalAction);
		msg.put("chosenAction", action);

		send(msg.toJSONString());
	}

	@SuppressWarnings("unchecked")
	public void sendInfo(String gameId, String playerId) {
		JSONObject msg = new JSONObject();
		msg.put("gameId", gameId);
		msg.put("playerId", playerId);
		msg.put("action", "info");

		send(msg.toJSONString());
	}

	private void send(String jsonString) {
		System.out.println("Sending : " + jsonString);

		BufferedReader in = null;
		BufferedWriter out = null;
		Socket server = null;
		try {

			server = new Socket("localhost", SERVER_PORT);

			in = new BufferedReader(new InputStreamReader(
					server.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(
					server.getOutputStream()));

			// send request
			out.write(jsonString);
			out.newLine();
			out.write("#END#");
			out.newLine();
			out.flush();

			StringBuffer response = new StringBuffer();

			String line = null;
			while ((line = in.readLine()) != null) {
				if (!"#END#".equals(line)) {
					response.append(line);
				} else {
					// stop reading
					break;
				}
			}

			Main.getWindow().processResponse(response.toString());

		} catch (IOException e) {
			System.err.println("An error occured");
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
				if (server != null) {
					server.close();
				}
			} catch (IOException e) {
				System.err.println("An error occured");
				e.printStackTrace();
			}
		}

	}

	public void sendStart(String gameId, String playerId) {
		JSONObject msg = new JSONObject();
		msg.put("gameId", gameId);
		msg.put("playerId", playerId);
		msg.put("action", "start");

		send(msg.toJSONString());
	}

	public void sendEnd(String gameId, String playerId) {
		JSONObject msg = new JSONObject();
		msg.put("gameId", gameId);
		msg.put("playerId", playerId);
		msg.put("action", "end");

		send(msg.toJSONString());
	}

}
